package com.zhaohanlin.start;

import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.sql.SQLOutput;
import java.util.Random;

/**
 * @author zhaohanlin
 */

public class TestClss {
    /**
     * String	concat​(String str)
     * Concatenates the specified string to the end of this string.
     * 将指定的字符串连接到该字符串的末尾。
     */
    @Test
    public void concat() {
        String str = "ABC";
        String result = str.concat("DE");
        System.out.println(result);
    }

    /**
     * boolean	contains​(String s)
     * Returns true if and only if this string contains the specified sequence of char values.
     * 当且仅当此字符串包含指定的char值序列时，才返回true。
     */
    @Test
    public void contains() {
        String str = "ABC";
        boolean result = str.contains("AC");
        System.out.println(result);
    }

    /**
     * boolean	endsWith​(String suffix)
     * Tests if this string ends with the specified suffix.
     * 测试此字符串是否以指定的后缀结尾
     */
    @Test
    public void endsWith() {
        String str = "ABC";
        boolean result = str.endsWith("BC");
        System.out.println(result);
    }

    /**
     * boolean	startsWith​(String prefix)
     * Tests if this string starts with the specified prefix.
     * 测试此字符串是否以指定的前缀开头。
     */
    @Test
    public void startsWith() {
        String str = "ABC";
        boolean result = str.startsWith("B");
        System.out.println(result);
    }

    /**
     * boolean	startsWith​(String prefix)
     * Tests if this string starts with the specified prefix.
     * 测试此字符串是否以指定的前缀开头。
     */
    @Test
    public void startsWith1() {
        String str = "ABC";
        boolean result = str.startsWith("BC");
        System.out.println(result);
    }

    /**
     * boolean	equals​(String str)
     * Compares this string to the specified object.
     * 将此字符串与指定字符串进行比较。
     */
    @Test
    public void equals() {
        String str = "ABC";
        boolean result = equals("abc");
        System.out.println(result);
    }

    /**
     * boolean	equalsIgnoreCase​(String anotherString)
     * Compares this String to another String, ignoring case considerations.
     * 不考虑大小写，将此字符串与另一个字符串进行比较。
     */
    @Test
    public void equalsIgnoreCase() {
        String str = "ABC";
        boolean result = str.equalsIgnoreCase("abc");
        System.out.println(result);
    }


    /**
     * boolean	isBlank()
     * Returns true if the string is empty or contains only white space codepoints, otherwise false.
     * 如果字符串为空或仅包含空格，则返回true，否则返回false。
     */
    @Test
    public void isBlank() {
        String str = "        ";
        boolean result = str.isBlank();
        System.out.println(result);
    }


    /**
     * int	indexOf​(String str)
     * Returns the index within this string of the first occurrence of the specified substring.
     * 返回第一次出现的指定子字符串在此字符串中的索引。
     */
    @Test
    public void indexOf() {
        String str = "ABC";
        int result = str.indexOf("A");
        System.out.println(result);
    }

    /**
     * int	lastIndexOf​(String str)
     * Returns the index within this string of the last occurrence of the specified substring.
     * 返回最后一次出现的指定子字符串在此字符串内的索引。
     */
    @Test
    public void lastIndexOf() {
        String str = "ABC";
        int result = str.lastIndexOf("A");
        System.out.println(result);
    }

    /**
     * boolean	isEmpty()
     * Returns true if, and only if, length() is 0.
     * 当且仅当字符串长度为0时，才返回true。
     */
    @Test
    public void isEmpty() {
        String str = "        ";
        boolean result = str.isEmpty();
        System.out.println(result);
    }

    /**
     * int	length()
     * Returns the length of this string.
     * 返回此字符串的长度。
     */
    @Test
    public void length() {
        String str = "林JUNJIE";
        int result = str.length();
        System.out.println(result);
    }


    /**
     * String	replace​(char oldChar, char newChar)
     * Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
     * 返回一个字符串，该字符串是用newChar替换此字符串中所有出现的oldChar的结果。
     */
    @Test
    public void replace() {
        String str = "ABCA";
        String result = str.replace("A", "a");
        System.out.println(result);
    }


    /**
     * String	substring​(int beginIndex, int endIndex)
     * Returns a string that is a substring of this string.
     * 返回一个字符串，该字符串是该字符串的子字符串。
     */
    @Test
    public void substring() {
        String str = "ABCD";
        String result = str.substring(1, 2);
        System.out.println(result);
    }

    /**
     * String	repeat​(int count)
     * Returns a string whose value is the concatenation of this string repeated count times.
     * 返回一个字符串，其值是此字符串的重复计数次数的串联。
     */
    @Test
    public void repeat() {
        String str = "A";
        String result = str.repeat(2);
        System.out.println(result);
    }

    /**
     * String	toLowerCase()
     * Converts all of the characters in this String to lower case using the rules of the default locale.
     * 将此字符串中的所有字符转换为小写。
     */
    @Test
    public void toLowerCase() {
        String str = "ABCD";
        String result = str.toLowerCase();
        System.out.println(result);
    }

    /**
     * String	toUpperCase()
     * Converts all of the characters in this String to upper case using the rules of the default locale.
     * 将此字符串中的所有字符转换为大写。
     */
    @Test
    public void toUpperCase() {
        String str = "abcd";
        String result = str.toUpperCase();
        System.out.println(result);
    }

    /**
     * String	trim()
     * Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
     * 返回一个值为该字符串的字符串，并删除所有前导和尾随空格。
     */
    @Test
    public void trim() {
        String str = "    abcd  ";
        String result = str.trim();
        System.out.println(result);
    }

    /**
     * static String	format​(String format, Object... args)
     * Returns a formatted string using the specified format string and arguments.
     * 使用指定的格式字符串和参数返回格式化的字符串。
     */
    @Test
    public void format() {
        String str = "我叫%s，我今年%d岁了,月薪%.2f元";

        // 所有的static修饰的静态方法直接用类名调用
        String result = String.format(str, "赵四", 58, 100000.00);
        System.out.println(result);
    }

    /**
     * static String	valueOf​(boolean b)
     * Returns the string representation of the boolean argument.
     * 返回布尔参数的字符串表示形式
     */
    @Test
    public void valueOf() {
        // 相当于 ("" + true)
        String result = String.valueOf(true);
        System.out.println(result);
    }

    /**
     * int	indexOf​(String str)
     * Returns the index within this string of the first occurrence of the specified substring.
     * 返回第一次出现的指定子字符串在此字符串中的索引。
     */
    @Test
    public void indexOf3() {
        String str = "abcabcabc";
        int result = str.indexOf("a");
        System.out.println(result);
    }

    /**
     * int	lastIndexOf​(String str)
     * Returns the index within this string of the last occurrence of the specified substring.
     * 返回最后一次出现的指定子字符串在此字符串内的索引。
     */
    @Test
    public void lastIndexOf3() {
        String str = "abcabcabc";
        int result = str.lastIndexOf("a");
        System.out.println(result);
    }

    /**
     * char	charAt​(int index)
     * returns the char value at the specified index.
     * 返回指定索引处的char值。
     */
    @Test
    public void charAt() {
        String str = "abcabcabc";
        // java中的调用使用分量符
        // alt + enter > enter
        char result = str.charAt(4);
        System.out.println(result);
    }

    /**
     * String	trim()
     * Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
     * 返回一个值为该字符串的字符串，并删除所有前导和尾随空格。
     */
    @Test
    public void trim1() {
        String str = "   abc abc abc  ";
        String result = str.trim();
        System.out.println(result);
    }

    /**
     * String	repeat​(int count)
     * Returns a string whose value is the concatenation of this string repeated count times.
     * 返回一个字符串，其值是此字符串的重复计数次数的串联。
     */
    @Test
    public void substring1() {
        String str = "abcabcabc";
        String result = str.substring(2, 5);
        System.out.println(result);
    }

    /**
     * String	trim()
     * Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
     * 返回一个值为该字符串的字符串，并删除所有前导和尾随空格。
     */
    @Test
    public void trim2() {
        String str = " abc ABCabc ";
        String result = str.trim();
        System.out.println(result);
    }

    /**
     * String	toLowerCase()
     * Converts all of the characters in this String to lower case using the rules of the default locale.
     * 将此字符串中的所有字符转换为小写。
     */
    @Test
    public void toLowerCase1() {
        String str = "abcABCabc";
        String result = str.toLowerCase();
        System.out.println(result);
    }

    public class PackingTest {

        @Test
        public void boxing() {
            Byte by = 100;
            Short sh = 200;
            Integer in = 300;
            Long lo = 400L;
            Double dou = 3.5;
            Float fl = 3.6F;
            Boolean flag = true;
            Character ch = '5';

            System.out.println(by);
            System.out.println(sh);
            System.out.println(in);
            System.out.println(lo);
            System.out.println(dou);
            System.out.println(fl);
            System.out.println(flag);
            System.out.println(ch);
        }
    }

    @Test
    public void unBoxing() {

        Byte by = 100;
        byte b = by;
        System.out.println(b);
    }

    @Test
    public void baseOperator() {
        System.out.println(10 / 4);
        System.out.println(10.0 / 4);
        System.out.println(-5 % 3);
        System.out.println(5 % 3);
        int a = 1;
        System.out.println(a++);
        System.out.println(++a);
        short sh = 100;
        sh = sh++;
        System.out.println(sh);
        sh = ++sh;
        System.out.println(sh);
    }

    @Test
    public void assignOperator() {
        int a = 1;
        System.out.println(a += 5);
        System.out.println(a -= 4);
        System.out.println(a *= 3);
        System.out.println(a /= 5);
        System.out.println(a %= 7);
        System.out.println(a);
    }

    @Test
    public void relationOperator() {
        int a = 9;
        // 关系运算符不允许连算
        System.out.println(a > 11);
        System.out.println(a == 11);
        System.out.println(a != 11);
        System.out.println(a < 11);
        System.out.println(a >= 11);
        System.out.println(a <= 11);

    }

    @Test
    public void logicOperator() {

        int a = 7;

        // 与中有假则假，与中全真才真
        System.out.println(a < 9 && a == 7);

        // 或中有真则真，或中全假才假
        System.out.println(a == 9 || a != 7);

        // 非真则假，非假则真
        System.out.println(!(a == 9));

        System.out.println(a == 9 || a == 1 && a == 7);
    }

    @Test
    public void bitOperator() {
        int a = 10;
        System.out.println(a != 10 & a++ == 10);
        System.out.println(a);
    }

    @Test
    public void ternaryOperaor() {
        double a = 57;
        String result = a < 100 || a < 0 ? "有问题" : (a >= 80 ? "优秀" : (a >= 60 ? "及格" : "垃圾"));
        System.out.println(result);
    }

    @Test
    public void mathApi() {
        System.out.println(Math.max(1, 2));
        System.out.println(Math.min(1, 2));
        System.out.println(Math.ceil(1.1));
        System.out.println(Math.floor(1.9));
        System.out.println(Math.abs(-5));
        System.out.println(Math.round(-11.5));
        System.out.println(Math.pow(2, 3));
    }

    @Test
    public void aaa() {
        double b = 52;
        String result = b < 0 || b > 100 ? "有问题" : (b >= 61 ? "及格" : (b >= 81 ? "优秀" : "不及格"));

    }

    @Test
    public void aaaa() {
        int a = 1, b = 2;
        String result = a == b ? "相等" : (a < b ? "小" : "大");
        System.out.println(result);

    }

    @Test
    public void Test13() {
        int a = 4, b = 6;
        Object result = a == b ? "相等" : (a > b ? a : b);
        System.out.println(result);
    }

    @Test
    public void Test14() {
        int a = 1, b = 2;


        System.out.println(a);
        System.out.println(b);

    }

    @Test
    public void randomApi() {
        Random random = new Random();
        System.out.println(random.nextDouble());
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));
        System.out.println(random.nextInt(10));

    }

    @Test
    public void text01() {
        String str = "bcabcbc";
        System.out.println(str.indexOf("a") == str.lastIndexOf("a"));
    }

    @Test
    public void text02() {
        String str = "abc abc abc";
        str = str.replace(" ", "");
        System.out.println(str.substring(str.indexOf("b") + 1, str.lastIndexOf("b")));
    }

    @Test
    public void compare() {
        Random random = new Random();
        int numA = random.nextInt(100);
        int numB = random.nextInt(100);
        System.out.println("numA: " + numA);
        System.out.println("numB: " + numB);

        String result = numA > numB ? "numA大" :
                numA == numB ? "numA和numB相等" : "numA小";
        System.out.println(result);
    }

    @Test
    public void compare2() {
        Random random = new Random();
        int numA = random.nextInt(100);
        int numB = random.nextInt(100);
        System.out.println("numA: " + numA);
        System.out.println("numB: " + numB);

        String result = numA > numB ? String.valueOf(numA) :
                numA == numB ? "相等" : "" + numB;
        System.out.println(result);
    }

    @Test
    public void compare5() {
        Random random = new Random();
        int numA = random.nextInt(100);
        int numB = random.nextInt(100);
        System.out.println("numA: " + numA);
        System.out.println("numB: " + numB);

        String result = numA > numB ? String.valueOf(numA) :
                numA == numB ? "相等" : "" + numB;
        System.out.println(result);
    }

    @Test
    public void compare3() {
        Random random = new Random();
        int numA = random.nextInt(100);
        int numB = random.nextInt(100);
        System.out.println("numA: " + numA);
        System.out.println("numB: " + numB);

        String result = numA > numB ? String.valueOf(numA) :
                numA == numB ? "相等" : "" + numB;
        System.out.println(result);
    }

    @Test
    public void compare4() {
        Random random = new Random();
        System.out.println(random.nextInt(5 + 6));
        System.out.println(random.nextInt(5 + 6));
        System.out.println(random.nextInt(5 + 6));
        System.out.println(random.nextInt(5 + 6));
        System.out.println(random.nextInt(5 + 6));
    }
}